package net.zjitc.homework.homework10.phone;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @author 李广彦
 * @date 2019-05-06-21：51
 */
public class PhoneDaoDB implements PhoneDao {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;

    @Override
    public int insert(Phone phone) {
        String sql = "insert into phone(id,name,type,color) values(?,?,?,?)";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, phone.getId());
            preparedStatement.setString(2, phone.getName());
            preparedStatement.setString(3, phone.getType());
            preparedStatement.setString(4, phone.getColor());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public ArrayList<Phone> seleciAll() {
        ArrayList<Phone> arrayList = new ArrayList<Phone>();
        String sql = "select * from phone";
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String id = resultSet.getString("id");
                String name = resultSet.getString("name");
                String type = resultSet.getString("type");
                String color = resultSet.getString("color");
                Phone phone = new Phone(id, name, type, color);
                arrayList.add(phone);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arrayList;
    }

    @Override
    public Phone selectByID(String id) {
        Phone phone = null;
        String sql = "select * from phone where id=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, id);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                String name = resultSet.getString("name");
                String type = resultSet.getString("type");
                String color = resultSet.getString("color");
                phone = new Phone(id, name, type, color);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return phone;
    }

    @Override
    public int update(Phone phone) {
        String sql = "update phone set name=?,type=?,color=? where id=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, phone.getName());
            preparedStatement.setString(2, phone.getType());
            preparedStatement.setString(3, phone.getColor());
            preparedStatement.setString(4, phone.getId());
            return preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(String id) {
        String sql = "delete from phone where id=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, id);
            return preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void close() {
        JDBCUtils.close(resultSet, preparedStatement, connection);
    }
}
